import { ResourceScannerService } from '../services/resource-scanner.service';

function runDomainFilterTest() {
  console.log('=== 域名过滤测试 ===\n');

  const scanner = new ResourceScannerService();

  // 测试包含不同域名的数据
  const testProject = {
    screenshot: 'Fhx218a1hSHHnJJQ11FsgBGcwLRD',
    assets: [
      {
        package: {
          urls: [
            'https://cdn.appthen.com/lib.js',
            'https://res.wx.qq.com/lib.js', // 应该被过滤
            'https://external.com/lib.js' // 应该被过滤
          ]
        }
      }
    ]
  };

  const testDocuments = [
    {
      schema: JSON.stringify({
        backgroundImage: 'https://cdn.appthen.com/bg.jpg',
        externalApi: 'https://api.external.com/data.json', // 应该被过滤
        image: 'https://cdn.appthen.cn/product.jpg'
      })
    },
    {
      poster: 'https://external-site.com/poster.jpg', // 应该被过滤
      icon: 'Fv9W6G12tZzxM-hJjn1phf8vCOb5' // hash值，应该保留
    }
  ];

  const resources = scanner.scanProjectResources(testProject, testDocuments);

  console.log('找到的资源:');
  resources.forEach((resource, index) => {
    console.log(`  ${index + 1}. ${resource.originalUrl}`);
    console.log(`     类型: ${resource.type}`);
    console.log(`     来源: ${resource.source}`);
  });

  console.log(`\n总计: ${resources.length} 个资源`);

  // 验证没有外部资源
  const externalUrls = resources.filter(r =>
    r.originalUrl.startsWith('http') &&
    !r.originalUrl.includes('cdn.appthen.com') &&
    !r.originalUrl.includes('cdn.appthen.cn')
  );

  console.log(`外部URL数量: ${externalUrls.length}`);
  if (externalUrls.length > 0) {
    console.log('外部URL:');
    externalUrls.forEach(url => {
      console.log(`  - ${url}`);
    });
  } else {
    console.log('✅ 没有外部URL被扫描到');
  }

  // 验证预期的资源
  const expectedUrls = [
    'Fhx218a1hSHHnJJQ11FsgBGcwLRD',
    'https://cdn.appthen.com/lib.js',
    'https://cdn.appthen.com/bg.jpg',
    'https://cdn.appthen.cn/product.jpg',
    'Fv9W6G12tZzxM-hJjn1phf8vCOb5'
  ];

  const foundUrls = resources.map(r => r.originalUrl);
  const allExpectedFound = expectedUrls.every(url => foundUrls.includes(url));

  console.log(`\n预期资源: ${expectedUrls.length} 个`);
  console.log(`实际资源: ${foundUrls.length} 个`);
  console.log(`资源匹配: ${allExpectedFound ? '✅ 通过' : '❌ 失败'}`);
}

// 运行测试
if (require.main === module) {
  runDomainFilterTest();
}

export { runDomainFilterTest };